package Database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Model.CommitteeStaff;

public class CommitteeStaffDB {

	public static void updateCommitteeStaff(CommitteeStaff staff) {
		Connection con = DBConnection.connect(true);
		PreparedStatement stmt = null;
		String select = "UPDATE committeestaff SET Committee_ID = ?, CommitteeRole_ID = ? WHERE CommitteeStaff_ID = ?";

		try {
			stmt = con.prepareStatement(select);
			stmt.setString(1, staff.getCommittee_IDs());
			stmt.setString(2, staff.getCommitteeRole_IDs());
			stmt.setString(3, staff.getId());

			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
	}
	
	public static String getCommitteeID(String staffID) {
		Connection con = DBConnection.connect(true);
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String select = "SELECT Committee_ID FROM committeestaff WHERE CommitteeStaff_ID = ?";
		String committeeID = null;
		
		try {
			stmt = con.prepareStatement(select);
			stmt.setString(1, staffID);

			rs = stmt.executeQuery();
			
			while (rs.next()) {
				committeeID = rs.getString("Committee_ID");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		
		return committeeID;
	}

	public static String getCommitteeRoleID(String staffID) {
		Connection con = DBConnection.connect(true);
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String select = "SELECT CommitteeRole_ID FROM committeestaff WHERE CommitteeStaff_ID = ?";
		String committeeID = null;
		
		try {
			stmt = con.prepareStatement(select);
			stmt.setString(1, staffID);

			rs = stmt.executeQuery();
			
			while (rs.next()) {
				committeeID = rs.getString("CommitteeRole_ID");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		
		return committeeID;
	}

}
